package com.sino.androiddesign.recyclerviewdemo


import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView

import com.sino.androiddesign.R
import kotlinx.android.synthetic.main.fragment_switch_position.*
import java.util.*
import kotlin.collections.ArrayList

/**
 * 长按切换移动
 *
 */
class SwitchPositionFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_switch_position, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        switch_rv.layoutManager = GridLayoutManager(context!!, 4)
        var datas = ArrayList<String>()
        for (i in 0 until 20) {
            datas.add("apple$i")
        }
        var switchAdapter = SwitchAdapter(context!!, datas)
        switch_rv.adapter = switchAdapter

        var switchItemTouchHelper = SwitchItemTouchHelper(datas)
        var itemTouchHelper = ItemTouchHelper(switchItemTouchHelper)
        itemTouchHelper.attachToRecyclerView(switch_rv)
    }
}

class SwitchHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
    var textTv: TextView? = null

    fun bindView(itemView: View?) {
        textTv = itemView!!.findViewById(R.id.switch_item_tv)
    }
}

class SwitchAdapter(private var context: Context, private var datas: List<String>) : RecyclerView.Adapter<SwitchHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SwitchHolder {
        var inflate = LayoutInflater.from(context).inflate(R.layout.fragment_switch_item, parent, false)
        var switchHolder = SwitchHolder(inflate)
        switchHolder.bindView(inflate)
        return switchHolder
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    override fun onBindViewHolder(holder: SwitchHolder, position: Int) {
        holder.textTv!!.text = datas[position]
    }

}

/**
 * 总结:
 * 对于长按拖拽切换位置:
 * 首先需要实现 getMovementFlags 的标志
 * 网格布局和瀑布流布局 可以使用 (Up,DOwn,Left,right)
 *  条形布局可以使用 (up down  start end)
 *  确保isLongPressDragEnabled 返回true
 *  在onMove 的时候 就是更换位置
 *  makeMovementFlags(a,b )a 是拖拽的标志 , b 是滑动的标志
 */
class SwitchItemTouchHelper(var datas: List<String>) : ItemTouchHelper.Callback() {

    /**
     * 设置滑动类型标记
     */
    override fun getMovementFlags(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?): Int {

        var layoutManager = recyclerView!!.layoutManager
        println("110 getMovementFlags= ${layoutManager is LinearLayoutManager} ${layoutManager is GridLayoutManager} ")
        if (layoutManager is GridLayoutManager) {   //只能上下
            var dragFlag = ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
            println("110 GridLayoutManager= $dragFlag ")
            return makeMovementFlags(dragFlag, 0)
        } else if (layoutManager is LinearLayoutManager) {
            var dragFlag: Int = ItemTouchHelper.UP or ItemTouchHelper.DOWN
            println("110 LayoutManager= $dragFlag ")
            return makeMovementFlags(dragFlag, 0)
        } else {
            return makeMovementFlags(0, 0)
        }
    }

    /**
     * 拖拽item 的时候的回调
     */
    override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?): Boolean {
        var firstPosition = viewHolder!!.adapterPosition
        var targetPosition = target!!.adapterPosition
        println("110 firstPosition= $firstPosition  target =  $targetPosition ")
        Collections.swap(datas, firstPosition, targetPosition)
        recyclerView!!.adapter.notifyItemMoved(firstPosition, targetPosition)
        if (firstPosition < targetPosition) {
            recyclerView!!.adapter.notifyItemMoved(targetPosition - 1, firstPosition)
        } else {
            recyclerView!!.adapter.notifyItemMoved(targetPosition + 1, firstPosition)
        }
        return true
    }

    /**
     * item 被选中的时候的回调
     */
    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        super.onSelectedChanged(viewHolder, actionState)
    }

    /**
     * 滑动item
     */
    override fun onSwiped(viewHolder: RecyclerView.ViewHolder?, direction: Int) {
    }

    /**
     * 设置item 是否可以长按
     */
    override fun isLongPressDragEnabled(): Boolean {
        return true
    }

    /**
     * 设置item 是否可以滑动
     */
    override fun isItemViewSwipeEnabled(): Boolean {
        return super.isItemViewSwipeEnabled()
    }
}
